package com.example.myFirstProject.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.myFirstProject.entity.User;
import com.example.myFirstProject.mapper.UserMapper;
import com.example.myFirstProject.service.UserService;
import com.example.myFirstProject.utils.MailUtils;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper usermapper;
	
	private List<User> list;
	
	private User user1;
	
	@Autowired
	private MailUtils mail;
	
	
	
	public User Login(User user){
		
		return null;
	}
	
	public int insertUser(User user){
		
		if(user.getLastlogintime()==null){
			user.setLastlogintime(user.getRegistertime());
		}
		int as= usermapper.insertUser(user);
		if(as==1){
			String content=String.format("<!DocType html><html><head><meta charset='utf-8'/><title>邮件验证界面</title></head><body>您好:%s,您需要点击此处<a href='http://localhost:8080/getUsers/registerUser?uuid=%s'>点击这里</a></body></html>进行邮箱验证,谢谢合作", new Object[]{user.getUserName(),user.getUuid()});
			mail.sendHtmlMail(user.getEmail(),"注册用户的邮箱验证邮件",content);
		}
		return as;
	    
	}
	
	public int deleteUser(Integer id){
		return usermapper.deleteUser(id);
	}

	
	public int updateUser(User user) {
		
		return usermapper.updateUser(user);
	}

	
	public int updatePassword(String password) {
		
		//password验证
		return usermapper.updatePassword(password);
	}

	
	public int updateName(User user) {
		
		//userName验证
		return usermapper.updateName(user);
	}

	
	public int updateUUID(User user) {
		
		return usermapper.updateUUID(user);
	}

	
	public List<User> selectUser() {
		list = new ArrayList<User>();
		list = usermapper.selectUser();
		return list;
	}

	
	public User selectOneUser(Integer id) {
		user1=new User();
		user1=usermapper.selectOneUser(id);
		return user1;
	}

	
	public User selectNameAndPassword(User user) {
		user1 = new User();
		user1 = usermapper.selectNameAndPassword(user);
		return user1;
	}

	
	public int selectUid(User user) {
		
		return usermapper.selectUid(user);
	}

	
	public User selectUUID(String uuid) {
		
		return usermapper.selectUUID(uuid);
	}

	
	public String selectUUIDAndName(String loginname) {
		String uuidAndName = usermapper.selectUUIDAndName(loginname);
		
		return uuidAndName;
	}
	
}
